import { ref } from 'vue'
import {
  addOrUpdateReturnFactoryModule,
  getReturnFactoryAddressList,
  getReturnFactoryModuleList,
  modulesDel,
  modulesUpdateStatus,
  getModuleDetail as getModuleDetailAPI,
  modulesUpdateMobileShow as multipleUpdateMobileShowAPI,
  modulesUpdateReturnFactoryCheckMethod,
  modulesUpdateFaults,
  modulesEnableSnCodeCheck,
  modulesEnableSendBeforeIncome,
  modulesEnableFeeConfirm,
  modulesEnableInspectProcess,
  modulesEnableServiceReport,
  getReturnFactoryModuleOptions,
  getReturnFactoryModuleDetailBeforeUpdate,
  returnFactoryModuleConnectFieldSave,
  returnFactoryConnectFieldListDel,
  returnFactoryConnectFieldListMobileShow,
  returnFactoryConnectFieldListRequired,
  getReturnFactoryCustomFieldOptions,
  getReturnFactoryModuleConnectFieldList,
  getReturnFactoryModuleCustomFieldConnectDetail,
  updateNetworkSelectStatus,
  updateReturnFactoryModuleLogisticsWriteStatusByIds,
  updateReturnFactoryModuleReceiptByIds,
  moduleDetectFieldConnect,
  getDetectCustomFieldOptions,
  getModuleDetectFieldList,
  getModuleDetectFieldConnectDetail,
  moduleDetectFieldConnectDel,
  moduleConnectDetectFieldMobileShowUpdateByIds,
  moduleConnectDetectFieldRequiredUpdateByIds,
} from '@/api/return-factory'

export const useReturnFactoryModuleForm = () => {
  const returnFactoryModuleSaveLoading = ref(false)
  const getFormLoading = ref(false)
  const form = ref<ReturnFactoryAPIRequest.ModuleForm>({
    enabledCheckProcess: true,
    enabledFault: true,
    enabledFirstSendAfterIncom: true,
    enabledServiceReport: true,
    enabledSnCodeCheck: true,
    id: '',
    mobileShow: true,
    name: '',
    repairAddrStoreId: '',
    repairCheckRoot: 1,
    repairCheckRootEmpIds: [],
    repairCheckType: 1,
    status: false,
    sureFee: true,
    enabledNetwork: false,
    enableWriteLogistics: true,
    enableRepairSignFor: true,
  })

  const returnFactoryModuleSave = async () => {
    returnFactoryModuleSaveLoading.value = true
    const res = await addOrUpdateReturnFactoryModule(form.value)
    returnFactoryModuleSaveLoading.value = false
    return res.data
  }

  const resetForm = () => {
    form.value = {
      enabledCheckProcess: true,
      enabledFault: true,
      enabledFirstSendAfterIncom: true,
      enabledServiceReport: true,
      enabledSnCodeCheck: true,
      id: '',
      mobileShow: true,
      name: '',
      repairAddrStoreId: '',
      repairCheckRoot: 1,
      repairCheckRootEmpIds: [],
      repairCheckType: 1,
      status: false,
      sureFee: true,
      enabledNetwork: false,
      enableWriteLogistics: false,
      enableRepairSignFor: false,
    }
  }

  const getForm = async (id: string) => {
    getFormLoading.value = true
    const res = await getReturnFactoryModuleDetailBeforeUpdate(id)
    getFormLoading.value = false
    form.value = res.data.data.row
    if (form.value.repairAddrStoreId === '0') {
      form.value.repairAddrStoreId = ''
    }
    if (!form.value.repairCheckRootEmpIds) {
      form.value.repairCheckRootEmpIds = []
    }
  }

  return {
    form,
    getFormLoading,
    returnFactoryModuleSaveLoading,
    getForm,
    resetForm,
    returnFactoryModuleSave,
  }
}

export const useGetAddressList = () => {
  const getAddressListLoading = ref(false)
  const getAddressListKeyword = ref('')
  const addressList = ref<Array<ReturnFactoryAddress>>([])
  const total = ref(0)

  const getAddressList = async () => {
    getAddressListLoading.value = true
    const res = await getReturnFactoryAddressList({
      keyword: getAddressListKeyword.value,
      page: 1,
      limit: 50,
    })
    getAddressListLoading.value = false
    addressList.value = res.data.data.rows
    total.value = Number(res.data.data.total)
  }

  return {
    total,
    addressList,
    getAddressListLoading,
    getAddressListKeyword,
    getAddressList,
  }
}

export const useGetModuleList = () => {
  const total = ref(0)
  const getModuleListLoading = ref(false)
  const getModuleListParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })
  const moduleList = ref<Array<ReturnFactoryModule>>([])

  const getModulesList = async () => {
    getModuleListLoading.value = true
    const res = await getReturnFactoryModuleList(getModuleListParam.value)
    getModuleListLoading.value = false
    total.value = Number(res.data.data.total)
    moduleList.value = res.data.data.rows
  }

  return {
    total,
    moduleList,
    getModuleListParam,
    getModuleListLoading,
    getModulesList,
  }
}

export const useGetModuleDetail = () => {
  const getModuleDetailLoading = ref(false)
  const moduleDetail = ref<ReturnFactoryModuleDetail>({
    enabledCheckProcess: true,
    enabledFault: true,
    enabledFirstSendAfterIncom: true,
    enabledServiceReport: true,
    enabledSnCodeCheck: true,
    id: '',
    mobileShow: true,
    name: '',
    repairAddrStoreId: '',
    repairCheckRoot: 1,
    repairCheckRootEmpIds: [],
    repairCheckType: 1,
    status: false,
    sureFee: true,
    createTime: '',
    updateTime: '',
    repairCheckRootEmps: [],
    enabledNetwork: false,
    enableRepairSignFor: false,
    enableWriteLogistics: false,
  })

  const getModuleDetail = async (id: string) => {
    getModuleDetailLoading.value = true
    const res = await getModuleDetailAPI(id)
    getModuleDetailLoading.value = false
    moduleDetail.value = res.data.data.row
    if (!moduleDetail.value.repairCheckRootEmps) {
      moduleDetail.value.repairCheckRootEmps = []
    }
  }

  return {
    moduleDetail,
    getModuleDetailLoading,
    getModuleDetail,
  }
}

export const useModulesDel = () => {
  const delLoading = ref(false)

  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await modulesDel(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

export const useModulesStatusUpdate = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesUpdateStatus(ids, false)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesUpdateStatus(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useModulesMobileShowOrHide = () => {
  const loading = ref(false)

  const show = async (ids: Array<string>) => {
    loading.value = true
    const res = await multipleUpdateMobileShowAPI(ids, true)
    loading.value = false
    return res.data
  }

  const hide = async (ids: Array<string>) => {
    loading.value = true
    const res = await multipleUpdateMobileShowAPI(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    show,
    hide,
  }
}

export const useModulesUpdateCheckMethod = () => {
  const loading = ref(false)

  const toWholeOrder = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesUpdateReturnFactoryCheckMethod(ids, 1)
    loading.value = false
    return res.data
  }

  const toAlone = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesUpdateReturnFactoryCheckMethod(ids, 2)
    loading.value = false
    return res.data
  }

  return {
    loading,
    toWholeOrder,
    toAlone,
  }
}

export const useModulesFaultsEnableOrDisable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesUpdateFaults(ids, true)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesUpdateFaults(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useModulesSnCodeEnableOrDisable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableSnCodeCheck(ids, true)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableSnCodeCheck(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useModulesSendBeforeIncomeEnableOrDisable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableSendBeforeIncome(ids, true)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableSendBeforeIncome(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useModulesFeeConfirmEnableOrDisable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableFeeConfirm(ids, true)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableFeeConfirm(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useModulesInspectProcessEnableOrDisable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableInspectProcess(ids, true)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableInspectProcess(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useModulesServiceReportEnableOrDisable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableServiceReport(ids, true)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await modulesEnableServiceReport(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useGetOptions = () => {
  const getOptionsLoading = ref(false)
  const getOptionsParam = ref({
    name: '',
    limit: 0,
    page: 0,
  })
  const options = ref<Array<ReturnFactoryModuleOption>>([])

  const getOptions = async () => {
    getOptionsLoading.value = true
    const res = await getReturnFactoryModuleOptions(getOptionsParam.value)
    getOptionsLoading.value = false
    options.value = res.data.data.rows
  }

  return {
    options,
    getOptionsLoading,
    getOptionsParam,
    getOptions,
  }
}

export const useGetReturnFactoryModuleConnectCustomFieldList = () => {
  const loading = ref(false)
  const getListParam = ref({
    limit: 50,
    page: 1,
  })
  const list = ref<Array<ReturnFactoryModuleCustomFieldConnect>>([])
  const total = ref(0)

  const getList = async (moduleId: string) => {
    loading.value = true
    const res = await getReturnFactoryModuleConnectFieldList(moduleId, getListParam.value)
    loading.value = false
    list.value = res.data.data.rows
    total.value = Number(res.data.data.total)
  }

  return {
    list,
    total,
    loading,
    getListParam,
    getList,
  }
}

export const useConnectFieldForm = () => {
  const form = ref<ReturnFactoryAPIRequest.ModuleConnectFieldForm>({
    id: '',
    mobileShow: true,
    repairFieldId: '',
    repairModuleId: '',
    required: true,
  })
  const submitLoading = ref(false)

  const resetForm = () => {
    form.value = {
      id: '',
      mobileShow: true,
      repairFieldId: '',
      repairModuleId: '',
      required: true,
    }
  }

  const submit = async () => {
    submitLoading.value = true
    const res = await returnFactoryModuleConnectFieldSave(form.value)
    submitLoading.value = false
    return res.data
  }

  return {
    form,
    submitLoading,
    submit,
    resetForm,
  }
}

export const useGetCustomFieldConnectDetail = () => {
  const loading = ref(false)
  const detail = ref<ReturnFactoryModuleCustomFieldConnectDetail>({
    id: '',
    mobileShow: true,
    repairFieldId: '',
    repairModuleId: '',
    required: true,
  })

  const getDetail = async (fieldId: string) => {
    loading.value = true
    const res = await getReturnFactoryModuleCustomFieldConnectDetail(fieldId)
    loading.value = false
    detail.value = res.data.data.row
  }

  return {
    detail,
    loading,
    getDetail,
  }
}

export const useConnectFieldListDel = () => {
  const loading = ref(false)

  const del = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryConnectFieldListDel(ids)
    loading.value = false
    return res.data
  }

  return {
    loading,
    del,
  }
}

export const useConnectFieldListMobileShow = () => {
  const loading = ref(false)

  const show = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryConnectFieldListMobileShow(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    show,
  }
}

export const useConnectFieldListMobileHide = () => {
  const loading = ref(false)

  const hide = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryConnectFieldListMobileShow(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    hide,
  }
}

export const useConnectFieldListRequired = () => {
  const loading = ref(false)

  const required = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryConnectFieldListRequired(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    required,
  }
}

export const useConnectFieldListUnRequired = () => {
  const loading = ref(false)

  const unRequired = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryConnectFieldListRequired(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    unRequired,
  }
}

export const useGetCustomFieldOptions = () => {
  const loading = ref(false)
  const getOptionsParam = ref({
    name: '',
    limit: 50,
    page: 1,
  })
  const options = ref<Array<ReturnFactoryCustomFieldOption>>([])
  const getOptions = async () => {
    loading.value = true
    const res = await getReturnFactoryCustomFieldOptions(getOptionsParam.value)
    options.value = res.data.data.rows
    loading.value = false
  }

  return {
    loading,
    options,
    getOptionsParam,
    getOptions,
  }
}

/**
 * 批量允许选择网点
 */
export const useMultipleEnabledSelectNetwork = () => {
  // 允许加载
  const loading = ref(false)

  /**
   * 允许选择网点
   * @param {Array<string>} ids - 返厂模块id集合
   */
  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await updateNetworkSelectStatus(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
  }
}

/**
 * 批量不允许选择网点
 */
export const useMultipleDisabledSelectNetwork = () => {
  // 不允许加载
  const loading = ref(false)

  /**
   * 不允许选择网点
   * @param {Array<string>} ids - 返厂模块id集合
   */
  const disabled = async (ids: Array<string>) => {
    loading.value = true
    const res = await updateNetworkSelectStatus(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    disabled,
  }
}

/**
 * 返厂模块填写物流批量开启
 */
export const useWriteLogisticsEnableByIds = () => {
  // 开启加载
  const enableLoading = ref(false)

  /**
   * 开启填写物流
   * @param {Array<string>} ids - 模块id集合
   */
  const enableWriteLogistics = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await updateReturnFactoryModuleLogisticsWriteStatusByIds(ids, true)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enableWriteLogistics,
  }
}

/**
 * 返厂模块填写物流批量关闭
 */
export const useWriteLogisticsDisableByIds = () => {
  // 关闭加载
  const disableLoading = ref(false)

  /**
   * 关闭填写物流
   * @param {Array<string>} ids - 模块id集合
   */
  const disableWriteLogistics = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await updateReturnFactoryModuleLogisticsWriteStatusByIds(ids, false)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disableWriteLogistics,
  }
}

/**
 * 返厂模块签收批量开启
 */
export const useReceiptEnableByIds = () => {
  // 开启加载
  const enableLoading = ref(false)

  /**
   * 开启签收
   * @param {Array<string>} ids - 模块id集合
   */
  const enableReceipt = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await updateReturnFactoryModuleReceiptByIds(ids, true)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enableReceipt,
  }
}

/**
 * 返厂模块签收批量关闭
 */
export const useReceiptDisableByIds = () => {
  // 关闭加载
  const disableLoading = ref(false)

  /**
   * 关闭签收
   * @param {Array<string>} ids - 模块id集合
   */
  const disableReceipt = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await updateReturnFactoryModuleReceiptByIds(ids, false)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disableReceipt,
  }
}

/**
 * 模块关联字段保存
 */
export const useDetectFieldConnect = () => {
  // 关联加载
  const connectLoading = ref(false)

  // 关联表单
  const form = ref<ReturnFactoryAPIRequest.ModuleDetectFieldConnect>({
    id: '',
    mobileShow: true,
    repairFieldId: '',
    repairModuleId: '',
    required: false,
  })

  /**
   * 关联方法
   */
  const connect = async () => {
    connectLoading.value = true
    const res = await moduleDetectFieldConnect(form.value)
    connectLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      id: '',
      mobileShow: true,
      repairFieldId: '',
      repairModuleId: '',
      required: false,
    }
  }

  return {
    form,
    connectLoading,
    connect,
    resetForm,
  }
}

/**
 * 获取检测自定义段下拉选项
 */
export const useGetDetectCustomFieldOptions = () => {
  // 获取下拉选项加载
  const getOptionsLoading = ref(false)

  // 获取下拉选项请求参数
  const getOptionsParams = ref({
    name: '',
    page: 1,
    limit: 50,
  })

  // 检测字段下拉选项
  const options = ref<Array<ReturnFactoryDetectFieldOptions>>([])

  /**
   * 获取下拉选项
   */
  const getOptions = async () => {
    getOptionsLoading.value = true
    const res = await getDetectCustomFieldOptions(getOptionsParams.value)
    options.value = res.data.data.rows
    getOptionsLoading.value = false
  }

  return {
    options,
    getOptionsParams,
    getOptionsLoading,
    getOptions,
  }
}

/**
 * 获取模块检测字段列表
 */
export const useGetModuleDetectFieldList = () => {
  // 获取字段列表加载
  const getFieldListLoading = ref(false)

  // 获取字段列表请求参数
  const getFieldListParams = ref({
    limit: 50,
    page: 1,
  })

  // 列表总条数
  const total = ref(0)

  // 字段列表
  const fieldList = ref<Array<ReturnFactoryDetectCustomField>>([])

  /**
   * 获取字段列表
   * @param {String} id - 返厂模块id
   */
  const getFieldList = async (id: string) => {
    getFieldListLoading.value = true
    const res = await getModuleDetectFieldList(id, getFieldListParams.value)
    total.value = Number(res.data.data.total)
    fieldList.value = res.data.data.rows
    getFieldListLoading.value = false
  }

  return {
    total,
    fieldList,
    getFieldListParams,
    getFieldListLoading,
    getFieldList,
  }
}

/**
 * 获取检测字段关联详情
 */
export const useGetDetectFieldConnectDetail = () => {
  // 获取检测字段关联详情加载
  const getDetailLoading = ref(false)

  // 字段关联详情
  const fieldConnectDetail = ref<ReturnFactoryModuleDetectFieldConnectDetail>({
    id: '',
    mobileShow: true,
    repairFieldId: '',
    repairModuleId: '',
    required: false,
  })

  /**
   * 获取详情
   * @param {String} id - 关联id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getModuleDetectFieldConnectDetail(id)
    fieldConnectDetail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    getDetailLoading,
    fieldConnectDetail,
    getDetail,
  }
}

/**
 * 根据id集合删除模块关联检测字段
 */
export const useModuleDetectFieldDelByIds = () => {
  // 删除加载
  const delLoading = ref(false)

  /**
   * 删除方法
   * @param {Array<string>} ids - 字段id集合
   */
  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await moduleDetectFieldConnectDel(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

/**
 * 根据id集合修改关联检测字段为必填
 */
export const useModuleDetectFieldRequired = () => {
  // 更新为必填加载
  const updateToRequiredLoading = ref(false)

  /**
   * 更新为必填
   * @param {Array<string>} ids - 关联字段id集合
   */
  const updateToRequired = async (ids: Array<string>) => {
    updateToRequiredLoading.value = true
    const res = await moduleConnectDetectFieldRequiredUpdateByIds(ids, true)
    updateToRequiredLoading.value = false
    return res.data
  }

  return {
    updateToRequiredLoading,
    updateToRequired,
  }
}

/**
 * 根据id集合修改关联检测字段为不必填
 */
export const useModuleDetectFieldUnRequired = () => {
  // 更新为不必填加载
  const updateToUnRequiredLoading = ref(false)

  /**
   * 更新为不必填
   * @param {Array<string>} ids - 关联字段id集合
   */
  const updateToUnRequired = async (ids: Array<string>) => {
    updateToUnRequiredLoading.value = true
    const res = await moduleConnectDetectFieldRequiredUpdateByIds(ids, false)
    updateToUnRequiredLoading.value = false
    return res.data
  }

  return {
    updateToUnRequiredLoading,
    updateToUnRequired,
  }
}

/**
 * 根据id集合修改关联检测字段为移动端显示
 */
export const useModuleDetectFieldMobileShow = () => {
  // 更新为移动端显示加载
  const updateToMobileShowLoading = ref(false)

  /**
   * 更新为移动端显示
   * @param {Array<string>} ids - 关联字段id集合
   */
  const updateToUnMobileShow = async (ids: Array<string>) => {
    updateToMobileShowLoading.value = true
    const res = await moduleConnectDetectFieldMobileShowUpdateByIds(ids, true)
    updateToMobileShowLoading.value = false
    return res.data
  }

  return {
    updateToMobileShowLoading,
    updateToUnMobileShow,
  }
}

/**
 * 根据id集合修改关联检测字段为移动端隐藏
 */
export const useModuleDetectFieldMobileHide = () => {
  // 更新为移动端隐藏加载
  const updateToMobileHideLoading = ref(false)

  /**
   * 更新为移动端隐藏
   * @param {Array<string>} ids - 关联字段id集合
   */
  const updateToUnMobileHide = async (ids: Array<string>) => {
    updateToMobileHideLoading.value = true
    const res = await moduleConnectDetectFieldMobileShowUpdateByIds(ids, false)
    updateToMobileHideLoading.value = false
    return res.data
  }

  return {
    updateToMobileHideLoading,
    updateToUnMobileHide,
  }
}
